/**
 * 完整视图组件（原 ViewComponent）
 * 
 * 职责：
 * 1. 提供所有类型的组件缓存（向后兼容）
 * 2. 支持自定义组件
 * 
 * 适用场景：
 * - 需要多种组件类型的复杂实体
 * - 向后兼容旧代码
 * 
 * 内存占用：~104 bytes
 * - 基类: ~40 bytes
 * - 2D 组件: ~24 bytes
 * - 物理组件: ~16 bytes
 * - 动画组件: ~8 bytes
 * - componentTypes: ~16 bytes
 * 
 * 注意：如果实体只需要特定类型的组件，建议使用专用的视图组件：
 * - ViewComponent2D: 只需要 2D 渲染
 * - ViewComponentPhysics: 只需要物理
 * - ViewComponentAnimation: 只需要动画
 * 
 * @author AI Assistant
 * @date 2025-11-29
 */

import { ecs } from "../../../../core/ecs/ECS";
import { Sprite, Label, Animation, UITransform, RigidBody2D, Collider2D, Component } from "cc";
import { ViewComponentBase } from "./ViewComponentBase";

/**
 * 视图组件类型枚举
 */
export enum ViewComponentType {
    /** 2D 渲染组件（Sprite, Label） */
    RENDER_2D = 'render2d',
    /** UI 组件（UITransform） */
    UI = 'ui',
    /** 动画组件（Animation） */
    ANIMATION = 'animation',
    /** 物理组件（RigidBody2D, Collider2D） */
    PHYSICS = 'physics',
    /** 自定义组件 */
    CUSTOM = 'custom'
}

/**
 * 完整视图组件（兼容旧 ViewComponent）
 */
@ecs.register('View', false)
export class ViewComponentFull extends ViewComponentBase {
    // ========== 懒加载缓存 ==========
    
    /** Sprite 组件 - 懒加载 */
    private _sprite: Sprite | null | undefined = undefined;
    
    /** Label 组件 - 懒加载 */
    private _label: Label | null | undefined = undefined;
    
    /** Animation 组件 - 懒加载 */
    private _animation: Animation | null | undefined = undefined;
    
    /** UITransform 组件 - 懒加载 */
    private _uiTransform: UITransform | null | undefined = undefined;
    
    /** RigidBody2D 组件 - 懒加载 */
    private _rigidbody: RigidBody2D | null | undefined = undefined;
    
    /** Collider2D 组件 - 懒加载 */
    private _collider: Collider2D | null | undefined = undefined;
    
    /** 组件类型标记 */
    public componentTypes: Set<ViewComponentType> = new Set();
    
    /** 自定义组件缓存 */
    private customComponents: Map<any, Component> = new Map();
    
    // ========== 懒加载 Getter ==========
    
    get sprite(): Sprite | null {
        if (this._sprite === undefined) {
            this._sprite = this.node?.getComponent(Sprite) || null;
            if (this._sprite) {
                this.componentTypes.add(ViewComponentType.RENDER_2D);
            }
        }
        return this._sprite;
    }
    
    get label(): Label | null {
        if (this._label === undefined) {
            this._label = this.node?.getComponent(Label) || null;
            if (this._label) {
                this.componentTypes.add(ViewComponentType.RENDER_2D);
            }
        }
        return this._label;
    }
    
    get animation(): Animation | null {
        if (this._animation === undefined) {
            this._animation = this.node?.getComponent(Animation) || null;
            if (this._animation) {
                this.componentTypes.add(ViewComponentType.ANIMATION);
            }
        }
        return this._animation;
    }
    
    get uiTransform(): UITransform | null {
        if (this._uiTransform === undefined) {
            this._uiTransform = this.node?.getComponent(UITransform) || null;
            if (this._uiTransform) {
                this.componentTypes.add(ViewComponentType.UI);
            }
        }
        return this._uiTransform;
    }
    
    get rigidbody(): RigidBody2D | null {
        if (this._rigidbody === undefined) {
            this._rigidbody = this.node?.getComponent(RigidBody2D) || null;
            if (this._rigidbody) {
                this.componentTypes.add(ViewComponentType.PHYSICS);
            }
        }
        return this._rigidbody;
    }
    
    get collider(): Collider2D | null {
        if (this._collider === undefined) {
            this._collider = this.node?.getComponent(Collider2D) || null;
            if (this._collider) {
                this.componentTypes.add(ViewComponentType.PHYSICS);
            }
        }
        return this._collider;
    }
    
    /**
     * 重置组件
     */
    reset(): void {
        super.reset();
        
        this._sprite = undefined;
        this._label = undefined;
        this._animation = undefined;
        this._uiTransform = undefined;
        this._rigidbody = undefined;
        this._collider = undefined;
        
        this.componentTypes.clear();
        this.customComponents.clear();
    }
    
    // ========== 自定义组件 ==========
    
    /**
     * 获取自定义组件（类型安全）
     */
    getCustomComponent<T extends Component>(componentType: new () => T): T | null {
        if (!this.node) return null;
        
        if (this.customComponents.has(componentType)) {
            return this.customComponents.get(componentType) as T;
        }
        
        const component = this.node.getComponent(componentType);
        if (component) {
            this.customComponents.set(componentType, component);
            this.componentTypes.add(ViewComponentType.CUSTOM);
            return component as T;
        }
        
        return null;
    }
    
    /**
     * 获取自定义组件（通过类名，不推荐）
     */
    getCustomComponentByName<T = any>(componentName: string): T | null {
        if (!this.node) return null;
        const component = this.node.getComponent(componentName);
        return component as T || null;
    }
    
    /**
     * 检查是否有指定类型的组件
     */
    hasComponentType(type: ViewComponentType): boolean {
        return this.componentTypes.has(type);
    }
    
    /**
     * 预加载指定类型的组件
     */
    preloadComponents(types: ViewComponentType[]): void {
        if (!this.node) return;
        
        for (const type of types) {
            switch (type) {
                case ViewComponentType.RENDER_2D:
                    this.sprite;
                    this.label;
                    break;
                case ViewComponentType.UI:
                    this.uiTransform;
                    break;
                case ViewComponentType.ANIMATION:
                    this.animation;
                    break;
                case ViewComponentType.PHYSICS:
                    this.rigidbody;
                    this.collider;
                    break;
            }
        }
    }
    
    // ========== 便捷方法（从旧 ViewComponent 继承）==========
    
    /**
     * 播放动画
     */
    playAnimation(animName: string, loop: boolean = false): void {
        if (!this.animation) {
            console.warn('[ViewComponentFull] 没有 Animation 组件');
            return;
        }
        
        const animState = this.animation.getState(animName);
        if (animState) {
            if (loop) {
                animState.wrapMode = 2;
            } else {
                animState.wrapMode = 1;
            }
            this.animation.play(animName);
        } else {
            console.warn(`[ViewComponentFull] 动画 "${animName}" 不存在`);
        }
    }
    
    /**
     * 停止动画
     */
    stopAnimation(): void {
        if (this.animation) {
            this.animation.stop();
        }
    }
}

// ========== 向后兼容：导出为 ViewComponent ==========

/**
 * @deprecated 请使用 ViewComponentFull，或根据需要使用专用组件：
 * - ViewComponent2D
 * - ViewComponentPhysics
 * - ViewComponentAnimation
 */
export const ViewComponent = ViewComponentFull;

